<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Admin;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class LoginController extends Controller
{

    public function index(Request $request)
    {
        return ['status' => false, 'msg'=>'尚未登录'];
        return view('student.login');
    }
    // get登陆页
    public function login()
    {
        if (session('admin.id') && session('admin_sms_check')==1) {
            $url = '/admin/index';
            $modules = DB::table('xpel_role_module')
                ->leftJoin('xpel_module', 'xpel_module.id', 'xpel_role_module.module_id')
                ->where('xpel_role_module.role_id', session('admin.role_id'))
                ->orderBy('sort')
                ->get();
            if ($modules->isNotEmpty()) {
                foreach ($modules as $module) {
                    if ($module->id == 1) {
                        break;
                    }
                    if (!empty($module->url)) {
                        $url = $module->url;
                        break;
                    }
                }
            }

            return redirect($url);
        }
        return view('admin.login-2');
    }

    public function ajaxLogin(Request $request)
    {
        $username = trim($request->input('username'));
        $password = trim($request->input('password'));
        if (empty($username) || empty($password)) {
            return fs403('用户名或密码错误');
        }
        // ->withTrashed()
        $user = Admin::where('unique_id', $username)->first();
        if (empty($user->password) || !password_verify($password, $user->password)) {
            return fs403('用户名或密码错误');
        }
        if($user->status != 1) {
            return fs403('账户不可用！');
        }
        // 1超级管理员2省代，超级管理员额外账号密码登录1,
        if (in_array($user->user_type, [1, 2]) || $user->user_type > 3) {

        } else {
            return fs403('无登录权限');
        }

        if ($user->user_type == 1) {
            // $user->id = 1;
        }
        $user->password = bcrypt($password);
        $user->save();

        $type = 1;
        $uids = $request->cookie('uids');
        if (!empty($uids)) {
            $uids = json_decode($uids, 1);
            if (in_array($user->id, $uids)) {
                $type = 1;
            }
        }
        session(['admin'=>$user, 'admin_sms_check'=>$type]);
        // ->cookie('test', '123', 60)->cookie('remember_id', json_encode($loginids), 60);
        return fs200d('登录成功！', ['type'=>$type, 'user_type'=>$user->user_type, 'phone_number'=>substr($user->phone_number, -4)]);
    }

    public function checkSmsCode(Request $request)
    {
        $phone_number = trim($request->input('phone_number'));
        $sms_code = trim($request->input('sms_code'));
        $extension =trim($request->input('extension'));
        $type = 2;
        $admin_id = session('admin.id');
        $user = Admin::find($admin_id);
        if (empty($user->id)) {
            session()->forget('admin');
            return fs403('账号不存在');
        }
        if (empty($user->phone_number)) {
            session()->forget('admin');
            return fs403('账号尚未绑定手机，请联系管理员绑定');
        }
        if ($user->phone_number != $phone_number) {
            return fs403('手机号码不正确，如忘记手机号码请联系管理员获取');
        }
        $bool = checkSmsCode($phone_number, $type, $sms_code, $extension);
        if (@$bool['status'] == 'SUCCESS') {
            session(['admin_sms_check' => 1]);
            $loginids = [];
            $uids = $request->cookie('uids');
            if (!empty($uids)) {
                $loginids = json_decode($uids, 1);
            }
            $loginids[] = $user->id;
            $loginids = array_unique($loginids);
            return fs200d('验证通过！',[])->cookie('uids', json_encode($loginids), 86400*365);
            return $bool;
        }
        return fs403(@$bool['msg']);

        return $bool;

    }

    public function getSmsCode(Request $request)
    {
        $phone_number = trim($request->input('phone_number'));
        $extension = trim($request->input('extension'));
        $admin_id = session('admin.id');
        $user = Admin::find($admin_id);
        if (empty($user->id)) {
            session()->forget('admin');
            return fs403('账号不存在');
        }
        if (empty($user->phone_number)) {
            session()->forget('admin');
            return fs403('账号尚未绑定手机，请联系管理员绑定');
        }
        if ($user->phone_number != $phone_number) {
            return fs403('手机号码不正确，如忘记手机号码请联系管理员获取');
        }
        return sendSMSCode($phone_number, 2, $extension);

    }

    // post登陆
    public function dologin(Request $request)
    {
        $t = $request->input('t');
        $ajax = $request->header('x-requested-with');
        if ($t == 'ajax') {
            $a = $request->input('a');
            if ($a == 'getSmsCode') {
                return $this->getSmsCode($request);
            } elseif ($a == 'checkSmsCode') {
                return $this->checkSmsCode($request);
            }
            return $this->ajaxLogin($request);
            return fs200('success', $request->input());
        }

        $username = $request->input('username');
        $password = $request->input('password');
        if (empty($username) || empty($password)) {
            return redirect()->back()->withInput()->with('trash', ['content'=>'用户名或密码错误', 'type' => 'error']);
        }
        $admin = [];
        if (!empty($admin[$username]) && $password==$admin[$username]) {
            $user = Admin::find(1);
            $user->username = $username;
            session(['admin'=>$user]);
        } elseif ($username=='super.developer01@gmail.com' && $password=="xPeLWarant8578TY") {
            return redirect()->back()->withInput()->with('trash', ['content'=>'无登录权限', 'type' => 'error']);
            // $user = Admin::find(1);
            // session(['admin'=>$user]);
        } else {
            # $user = DB::table('users')->where('unique_id', $username)->first();
            // ->withTrashed()
            $user = Admin::where('unique_id', $username)->first();
            if (empty($user) || !password_verify($password, $user->password)) {
                return redirect()->back()->withInput()->with('trash', ['content'=>'用户名或密码错误', 'type' => 'error']);
            }
            // 1超级管理员2省代，超级管理员额外账号密码登录1,
            if (!in_array($user->user_type, [1, 2])) {
                if ($user->user_type > 3) {

                } else {
                    return redirect()->back()->withInput()->with('trash', ['content'=>'无登录权限', 'type' => 'error']);
                }
            }
        }
        if ($user->user_type == 1) {
            // $user->id = 1;
        }
        if($user->status != 1) {
            return fs403('账户不可用！');
        }

        $user->password = bcrypt($password);
        $user->save();

        session(['admin'=>$user]);
        return redirect('/admin/index');
    }
    // 退出
    public function loginout()
    {
        $bool = session()->forget('admin');
        $bool = session()->forget('admin_sms_check');
        return redirect('/admin/login');
    }
}
